package cn.web;

import cn.constant.ApiConstant;
import cn.constant.Messages;
import cn.entity.User;
import cn.repository.UserRepository;
import cn.service.RealNameService;
import cn.service.UserService;
import cn.util.AuthUtils;
import cn.util.StringUtils;
import cn.web.model.ApiResult;
import cn.web.model.VmUser;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

/**
 * @author m  2023/1/27 - 14:20
 * Description:
 */
@Api(tags = "登录业务")
@RestController
@RequestMapping(ApiConstant.ROUTE_AUTH_ROOT)
public class AuthApiController {
    private final UserService service;
    private final RealNameService realNameService;

    @Autowired
    UserRepository userRepository;


    @Autowired
    public AuthApiController(UserService userService, RealNameService realNameService) {
        this.service = userService;
        this.realNameService = realNameService;
    }

    //登录
    @ApiOperation("登录")
    @PostMapping(value = ApiConstant.ROUTE_AUTH_LOGIN,produces = ApiConstant.API_PRODUCES)
    public ApiResult login(@RequestBody JSONObject json){
        String name = json.getString(ApiConstant.KEY_LOGIN_USER_NAME);
        String salt = service.getSalt(name);
        //校验用户
        User user = service.check(name,
                StringUtils.md5Encode(json.getString(ApiConstant.KEY_LOGIN_PASSWORD),salt));
        if(user==null){
            return new ApiResult(false, Messages.WRONG_PASSWORD.toString(),null);
        }
        //类型禁止
        if(user.getType()<0){
            return new ApiResult(false,Messages.USER_FORBIDDEN.toString(),null);
        }
        //封装成vmUser，记录到内存
        VmUser vmUser = VmUser.apiLogin(user);
        return new ApiResult(true,"登录", vmUser);
    }

    //注册
    @ApiOperation("注册")
    @PostMapping(value = ApiConstant.ROUTE_AUTH_REGISTER,produces = ApiConstant.API_PRODUCES)
    public ApiResult register(@RequestBody JSONObject json){
        String nick = json.getString(ApiConstant.KEY_REGISTER_NICK_NAME);
        String name = json.getString(ApiConstant.KEY_REGISTER_USER_NAME);
        String phone = json.getString(ApiConstant.KEY_REGISTER_PHONE);
        String email = json.getString(ApiConstant.KEY_REGISTER_EMAIL);
        String password = json.getString(ApiConstant.KEY_REGISTER_PASSWORD);
        boolean isApply = json.getBoolean(ApiConstant.KEY_REGISTER_APPLY);

        //随机uuid加密
        String salt = UUID.randomUUID().toString();

        //没有昵称默认用户名
        if(StringUtils.isEmpty(nick)){
            nick = name;
        }

        //属性为空则返回错误
        if(StringUtils.isEmpty(name)||StringUtils.isEmpty(phone)||StringUtils.isEmpty(password)){
            return new ApiResult(false,Messages.INVALID_FORMAT.toString(),null);
        }

        //校验邮箱
        if(!StringUtils.validEmail(email)){
            return new ApiResult(false, Messages.INVALID_FORMAT.toString(),null);
        }

        //校验电话号码
        if(!StringUtils.validPhone(phone)){
            return new ApiResult(false,Messages.INVALID_FORMAT.toString(),null);
        }
        //校验用户唯一
        if(service.isUserNameOccupied(name)){
            return new ApiResult(false,Messages.USER_EXISTS.toString(),null);
        }


        //存入
        User user = new User();
        user.setAvatar(null);
        user.setApply(isApply);
        user.setEmail(email);
        user.setUserName(name);
        user.setNickName(nick);
        user.setPhone(phone);
        user.setType(2);
        user.setApply(isApply);
        //密码加密加盐
        user.setPassword(StringUtils.md5Encode(password,salt));
        //存盐
        user.setSalt(salt);
        VmUser vmUser = VmUser.apiLogin(service.register(user));
        return new ApiResult(true,Messages.SUCCESS.toString(),vmUser);
    }

    //登出
    @ApiOperation("登出")
    @PostMapping(value = ApiConstant.ROUTE_AUTH_LOGOUT,produces = ApiConstant.API_PRODUCES)
    public ApiResult logout(@RequestParam String token){
        return new ApiResult(true, AuthUtils.logout(token),null);
    }


}
